package com.sinosoft.jwjw.component.controller.system;


import com.sinosoft.jwjw.base.BaseCrudController;
import com.sinosoft.jwjw.base.BaseCrudService;
import com.sinosoft.jwjw.component.model.system.SUser;
import com.sinosoft.jwjw.component.service.system.SUserService;

import com.sinosoft.jwjw.utils.ConvertUtil;
import com.sinosoft.jwjw.utils.ResultDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户管理控制层
 * @ver 系统自动生成代码
 */
@Controller
@RequestMapping(value = "/sUser")
public class SUserController extends BaseCrudController<SUser,String> {

    @Autowired
    SUserService sUserService;

    @Override
    public BaseCrudService<SUser,String> getBaseService() {
        return sUserService;
    }

    //新增编辑保存
    @ResponseBody
    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    public String save(HttpServletRequest request) {

        //获取参数
        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//


        try {
            if ("".equals(id)) {
                // 新增
                id = ConvertUtil.getUUID20();
                SUser sUser = new SUser();

                sUser = ConvertUtil.toObject(params.toString() , SUser.class);
                sUser.setId(id);

                // 默认值
                sUser.setCreateDate(ConvertUtil.toSqlDate(new Date()));
                sUser.setCreatorId("缓存UserID");
                sUser.setCreatorName("缓存UserName");
                sUser.setModifyDate(ConvertUtil.toSqlDate(new Date()));
                sUser.setModifyId("缓存UserID");
                sUser.setModifyName("缓存UserName");
                sUser.setIsState("1");
                sUser.setIsDel("0");

                result = sUserService.inserts(sUser);
            } else {
                // 编辑
                // 默认值
                params.put("modifyDate", ConvertUtil.toDateString(new Date()));
                params.put("modifyId", "缓存UserID");
                params.put("modifyName", "缓存UserName");
                result = sUserService.updateById(params);
            }
        } catch (Exception ee) {
            result.setCode(500);
            result.setMsg("保存失败");
            result.setInnermsg(ee.toString());
        } finally {
            return result.toString();
        }
    }

    //重置密码
    @ResponseBody
    @RequestMapping(value = "/resetpwd" , method = RequestMethod.POST)
    public String resetpwd() {

        //获取参数
        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//
        if("".equals(id)){
            result.setCode(500);
            result.setMsg("用户id不能为空");
            return result.toString();
        }

        result = sUserService.resetpwd(id);

        return result.toString();
    }


    //有效无效
    @ResponseBody
    @RequestMapping(value = "/setIsState" , method = RequestMethod.POST)
    public String setIsState() {

        //获取参数
        RequestParamUtils();
        ResultDetail result = new ResultDetail();

        //解析处理参数
        String id = ConvertUtil.toString(params.get("id"));//
        String isState = ConvertUtil.toString(params.get("isState"));
        if("".equals(id)){
            result.setCode(500);
            result.setMsg("用户id不能为空");
            return result.toString();
        }

        result = sUserService.setIsState(id , isState);

        return result.toString();
    }

}
